package com.example.demo.service;

import com.example.demo.mapper.StudentMapper;
import com.example.demo.model.entity.Student;
import com.example.demo.model.request.StudentQueryRequest;
import com.example.demo.model.response.StudentResponse;
import com.example.demo.util.ExcelExportUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@RequiredArgsConstructor // 构造器注入
public class StudentService {

    private final StudentMapper studentMapper;

    /**
     * 模块一：查询相似学生
     */
    public List<Student> getSimilarStudents(StudentQueryRequest request) {
        // 校验参数（示例：阈值不能为负）
        if (request.getScoreThreshold() < 0) {
            throw new RuntimeException("相似性阈值不能为负数");
        }
        return studentMapper.selectSimilarStudents(request);
    }

    /**
     * 模块二：查询同目标院校学生
     */
    public List<Student> getStudentsByTargetSchool(StudentQueryRequest request) {
        // 校验参数（示例：目标院校不能为空）
        if (request.getSavedTargetSchool() == null || request.getSavedTargetSchool().trim().isEmpty()) {
            throw new RuntimeException("目标院校不能为空");
        }
        return studentMapper.selectByTargetSchool(request);
    }

    /**
     * 整合两个模块结果（供Controller调用）
     */
    public StudentResponse queryStudentInfo(StudentQueryRequest request) {
        StudentResponse response = new StudentResponse();
        response.setSimilarStudents(getSimilarStudents(request));
        response.setTargetSchoolStudents(getStudentsByTargetSchool(request));
        return response;
    }

    public byte[] exportSimilarStudents(StudentQueryRequest request) throws IOException {
        // 1. 查询相似学生数据
        List<Student> students = getSimilarStudents(request);
        // 2. 调用工具类生成Excel
        return ExcelExportUtil.exportStudentsToExcel(students);
    }
}